package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC869 {
    public long startExecuteTime = System.currentTimeMillis();

    public static Map<Integer, List<String>> lenMap = new HashMap<>();

    static {
        for (int i = 0; i < 36; i++) {
            String num = String.valueOf((long) Math.pow(2, i));
            if (!lenMap.containsKey(num.length())) lenMap.put(num.length(), new ArrayList<>());
            lenMap.get(num.length()).add(num);
        }
    }

    class Solution {
        public boolean reorderedPowerOf2(int n) {
            String num = String.valueOf(n);
            for (String target : lenMap.get(num.length())) {
                if (isMatch(num, target)) return true;
            }

            return false;
        }

        public boolean isMatch(String cur, String target) {
            char[] curArr = cur.toCharArray(), targetArr = target.toCharArray();
            Arrays.sort(curArr);
            Arrays.sort(targetArr);
            String curStr = new String(curArr), targetStr = new String(targetArr);

            return curStr.equals(targetStr);
        }

    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.reorderedPowerOf2(46));
    }

    public static void main(String[] args) throws Exception {
        LC869 an = new LC869();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
